package org.lanqiao.sjx.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.bean.Education;
import org.lanqiao.bean.Work;
import org.lanqiao.sjx.dao.EducationDao;
import org.lanqiao.sjx.dao.UserDao;
import org.lanqiao.sjx.dao.impl.EducationImpl;
import org.lanqiao.sjx.dao.impl.UserImpl;
import org.lanqiao.tools.BaseServlet;

import com.google.gson.Gson;
import com.sun.org.apache.bcel.internal.generic.Select;

@WebServlet("/Educationservlet")
public class Educationservlet extends BaseServlet{
	EducationDao dao = new EducationImpl();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Object id = request.getSession().getAttribute("id");
		Object typeU = request.getSession().getAttribute("typeU");
		this.request = request;
		this.response = response;

		String type = getString("type");
		redata.setMsg("");
		
		if (!isNull("type")) {
			switch (type) {
			case "select":
				selectEdu();
				
				break;
			case "insert":
				insertEdu();
				break;
			case "update":
				updateEdu();
				break;
			default:
				break;
			}
		}
			
		else {
				redata.setCode(3);
				redata.setData(null);
			}
			System.out.println(redata);
			response.getWriter().write(new Gson().toJson(redata));
	}

	private void updateEdu() {
		System.out.println("updateEdu");
		Integer eid=getInt("id");
		Integer startYear=getInt("startYear");
		Integer endYear=getInt("endYear");
		String schoolName=getString("schoolName");
		Integer eduBackground=getInt("eduBackground");
		String majorName=getString("majorName");
		
		if (redata.getMsg()!="") {
			redata.setCode(3);
			redata.setData(null);
			System.out.println("参数不全");
			return;
		}
		
		Integer i=dao.updateEdu(new Education(startYear, endYear, schoolName, eduBackground, majorName, eid));
		if(i>0) {
			redata.setCode(2);
			redata.setData(i);
			redata.setMsg("修改成功");
			System.out.println("修改信息成功");
		}
		
	}

	private void selectEdu() {
		System.out.println("select");
		Integer userID=getInt("userID");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		
		List<Education> selectEducation = dao.selectEducation(userID);
		redata.setCode(2);
		redata.setData(selectEducation);
		redata.setMsg("查询成功");
		
	}

	private void insertEdu() {
		System.out.println("insertEdu");
		
		Integer userID=getInt("userID");
		Integer startYear=getInt("startYear");
		Integer endYear=getInt("endYear");
		String schoolName=getString("schoolName");
		Integer eduBackground=getInt("eduBackground");
		String majorName=getString("majorName");
		
		if (redata.getMsg()!="") {
			redata.setCode(3);
			redata.setData(null);
			System.out.println("参数不全");
			return;
		}
		
		Integer i=dao.insertEdu(new Education(userID, startYear, endYear, schoolName, eduBackground, majorName));
		if(i>0) {
			redata.setCode(2);
			redata.setData(i);
			redata.setMsg("添加成功");
			System.out.println("添加信息成功");
		}
		
	}

}
